package com.neuedu.io;

import java.io.*;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/2912:02
 */
public class MyFileInputStream {


    public static void main(String[] args) {
        //创建读取的字节流
        // readOneByte();

        //读取多个字节
        // readManyByte();

        //循环的读取内容 直到末尾
        readAllByte();

    }

    private static void readAllByte() {
        InputStream fileInputStream = null;
        try {

            File file = new File("D:\\upload\\西游记.txt");
             fileInputStream = new FileInputStream(file);


            byte  buffer[] = new byte[1024];
            StringBuffer stringBuffer = new StringBuffer();
            // 读取一个字节
            int len = -1;

            while((len = fileInputStream.read(buffer)) != -1 ){
                System.out.println("len = " + len);
                stringBuffer.append( new String(buffer,0,len));
            }

            System.out.println("stringBuffer = " +stringBuffer);



        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }

    private static void readManyByte() {
        InputStream fileInputStream = null;
        try {

            File file = new File("D:\\upload\\西游记.txt");
            fileInputStream = new FileInputStream(file);


            byte  buffer[] = new byte[1024];

            // 读取一个字节
            int len = fileInputStream.read(buffer);

            System.out.println("new String(buffer) = " + new String(buffer,0,len));
            System.out.println("len = " + len);


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }


    /***
     * 读取一个字节
     */
    private static  void readOneByte(){

        InputStream fileInputStream = null;
        try {

            File file = new File("D:\\upload\\西游记.txt");
            fileInputStream = new FileInputStream(file);


            // 读取一个字节
            int readByte = fileInputStream.read();
            System.out.println("\" (char)readByte\" = " +((char)readByte));

            readByte = fileInputStream.read();
            System.out.println("\" (char)readByte\" = " +((char)readByte));

            readByte = fileInputStream.read();
            System.out.println("\" (char)readByte\" = " +((char)readByte));



        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }


}
